package com.landinfo.web.rest;

import com.codahale.metrics.annotation.Timed;
import com.landinfo.service.SpecialMapServiceManageService;
import com.landinfo.web.rest.util.HeaderUtil;
import com.landinfo.service.dto.SpecialMapServiceManageDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;

import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing SpecialMapServiceManage.
 */
@RestController
@RequestMapping("/api")
public class SpecialMapServiceManageResource {

    private final Logger log = LoggerFactory.getLogger(SpecialMapServiceManageResource.class);

    private static final String ENTITY_NAME = "specialMapServiceManage";

    private final SpecialMapServiceManageService specialMapServiceManageService;

    public SpecialMapServiceManageResource(SpecialMapServiceManageService specialMapServiceManageService) {
        this.specialMapServiceManageService = specialMapServiceManageService;
    }

    /**
     * POST  /special-map-service-manages : Create a new specialMapServiceManage.
     *
     * @param specialMapServiceManageDTO the specialMapServiceManageDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new specialMapServiceManageDTO, or with status 400 (Bad Request) if the specialMapServiceManage has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/special-map-service-manages")
    @Timed
    public ResponseEntity<SpecialMapServiceManageDTO> createSpecialMapServiceManage(@RequestBody SpecialMapServiceManageDTO specialMapServiceManageDTO) throws URISyntaxException {
        log.debug("REST request to save SpecialMapServiceManage : {}", specialMapServiceManageDTO);
        if (specialMapServiceManageDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new specialMapServiceManage cannot already have an ID")).body(null);
        }
        SpecialMapServiceManageDTO result = specialMapServiceManageService.save(specialMapServiceManageDTO);
        return ResponseEntity.created(new URI("/api/special-map-service-manages/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /special-map-service-manages : Updates an existing specialMapServiceManage.
     *
     * @param specialMapServiceManageDTO the specialMapServiceManageDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated specialMapServiceManageDTO,
     * or with status 400 (Bad Request) if the specialMapServiceManageDTO is not valid,
     * or with status 500 (Internal Server Error) if the specialMapServiceManageDTO couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/special-map-service-manages")
    @Timed
    public ResponseEntity<SpecialMapServiceManageDTO> updateSpecialMapServiceManage(@RequestBody SpecialMapServiceManageDTO specialMapServiceManageDTO) throws URISyntaxException {
        log.debug("REST request to update SpecialMapServiceManage : {}", specialMapServiceManageDTO);
        if (specialMapServiceManageDTO.getId() == null) {
            return createSpecialMapServiceManage(specialMapServiceManageDTO);
        }
        SpecialMapServiceManageDTO result = specialMapServiceManageService.save(specialMapServiceManageDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, specialMapServiceManageDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /special-map-service-manages : get all the specialMapServiceManages.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of specialMapServiceManages in body
     */
    @GetMapping("/special-map-service-manages")
    @Timed
    public List<SpecialMapServiceManageDTO> getAllSpecialMapServiceManages() {
        log.debug("REST request to get all SpecialMapServiceManages");
        return specialMapServiceManageService.findAll();
    }

    /**
     * 获取所有生效的专题图层信息
     *
     * @return the ResponseEntity with status 200 (OK) and the list of specialMapServiceManages in body
     */
    @GetMapping("/special-map-service-manages/enable/{enable}")
    @Timed
    public List<SpecialMapServiceManageDTO> getAllSpecialMapServiceManagesByEnable(@PathVariable boolean enable) {
        log.debug("REST request to get all SpecialMapServiceManages By enable");
        return specialMapServiceManageService.findAllByEnable(enable);
    }

    /**
     * GET  /special-map-service-manages/:id : get the "id" specialMapServiceManage.
     *
     * @param id the id of the specialMapServiceManageDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the specialMapServiceManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/special-map-service-manages/{id}")
    @Timed
    public ResponseEntity<SpecialMapServiceManageDTO> getSpecialMapServiceManage(@PathVariable String id) {
        log.debug("REST request to get SpecialMapServiceManage : {}", id);
        SpecialMapServiceManageDTO specialMapServiceManageDTO = specialMapServiceManageService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(specialMapServiceManageDTO));
    }

    /**
     * 根据图层组名获取数据
     *
     * @param groupName the specialMapGroupName of the specialMapServiceManageDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the specialMapServiceManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/special-map-service-manages/groupName/{groupName}")
    @Timed
    public ResponseEntity<SpecialMapServiceManageDTO> getByGroupName(@PathVariable String groupName) {
        log.debug("REST request to get SpecialMapServiceManage : {}", groupName);
        SpecialMapServiceManageDTO specialMapServiceManageDTO = specialMapServiceManageService.getByGroupName(groupName);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(specialMapServiceManageDTO));
    }

    /**
     * 根据模板类型获取数据
     *
     * @param type the templateType of the specialMapServiceManageDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the specialMapServiceManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/special-map-service-manages/templateType/{type}")
    @Timed
    public List<SpecialMapServiceManageDTO> getByTemplateType(@PathVariable String type) {
        log.debug("REST request to get SpecialMapServiceManage : {}", type);
        return specialMapServiceManageService.getByTemplateType(type);
    }

    /**
     * 通过父级Id获取子级所有数据
     *
     * @param parentId the parentId of the specialMapServiceManageDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the specialMapServiceManageDTO, or with status 404 (Not Found)
     */
    @GetMapping("/special-map-service-manages/children/{parentId}")
    @Timed
    public List<SpecialMapServiceManageDTO> getChildrenByParentId(@PathVariable String parentId) {
        log.debug("REST request to get SpecialMapServiceManage : {}", parentId);
        return specialMapServiceManageService.getChildrenByParentId(parentId);
    }

    /**
     * DELETE  /special-map-service-manages/:id : delete the "id" specialMapServiceManage.
     *
     * @param id the id of the specialMapServiceManageDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/special-map-service-manages/{id}")
    @Timed
    public ResponseEntity<Void> deleteSpecialMapServiceManage(@PathVariable String id) {
        log.debug("REST request to delete SpecialMapServiceManage : {}", id);
        specialMapServiceManageService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
}
